#    This file is part of robo-ircbot.
#
#    robo-ircbot is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    robo-ircbot is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with robo-ircbot.  If not, see <http://www.gnu.org/licenses/>.

package App::Modular::Module::Greeter;
use strict;
use warnings;
use base qw(App::Modular::Module);

print ":: Greeter module loaded.\n";

# Load the greetings file.
my @greetings;
if (open(GREETS, "<data/greetings")) {
        @greetings = <GREETS>;
        close(GREETS);
} else {
        @greetings = ();
}

# Events we want to receive.
sub events { return "JOIN PRIVMSG"; }

sub help {
        return (
                "Greets incoming idlers with inspirational messages.",
                ".addgreet <text>",
                ".lsgreet",
                ".delgreet <num>"
        );
}

# PRIVMSG events.
sub event_privmsg
{
        shift;
        my $e = shift;

        my $auth = $e->robo->mod("Auth") || return;
        my $help = $e->robo->mod("Helper") || return;

        # Admin commands for greeter module.
        if ($help->trigger_switch("greeter", $e)) {
                return;
        }

        if ($auth->is_authed($e->from)) {
                # Add greeting.
                if ($e->data =~ /^\.addgreet (.+)$/) {
                        push(@greetings, $1);

                        if (open(GREETS, ">>data/greetings")) {
                                print GREETS $1."\n";
                                $e->s("PRIVMSG ".$e->dest." :Wrote greeting to file!");
                                close(GREETS);
                        } else {
                                $e->s("PRIVMSG ".$e->dest." :Could not write greeting to file!");
                        }
                # List greetings.
                } elsif ($e->data =~ /^\.lsgreet$/) {
                        my $i = 0;
                        foreach (@greetings) {
                                chomp;
                                $e->s("PRIVMSG ".$e->dest." :".$i++.") ".$_);
                                sleep(1);
                        }
                # Delete greeting.
                } elsif ($e->data =~ /^\.delgreet (\d+)$/) {
                        if ($greetings[$1]) {
                                delete $greetings[$1];

                                # Write array to file.
                                if (open(GREETS, ">data/greetings")) {
                                        foreach (@greetings) {
                                                chomp;
                                                print GREETS $_."\n" if ($_);
                                        }
                                        $e->s("PRIVMSG ".$e->dest." :Greeting deleted!  File updated.");
                                        close(GREETS);
                                } else {
                                        $e->s("PRIVMSG ".$e->dest." :Could not update greeting file!");
                                }
                        }
                }
        }
}

# JOIN event.
sub event_join
{
        shift;
        my $e = shift;

        my $help = $e->robo->mod("Helper") || return;
        my $nick = $help->parse_nick($e->from);

        # Return if this trigger has been disabled.
        if (!$e->cfg->get_mod("greeter/enabled")) { return; }

        # Don't show a greeting if it's me joining.
        if ($nick eq $e->cfg->nick) {
                return;
        }

        my $to = $e->to;
        my $greeting = $greetings[int rand($#greetings)];
        $greeting =~ s/\%nick\%/$nick/g;
        $greeting =~ s/\%chan\%/$to/g;

        chomp $greeting;
        $e->s("PRIVMSG ".$e->to." :".$greeting);
}

1;
